package model.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtil {
	
	private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("NutricaoFuzzy");
	private static EntityManager em = null;
	
	public static EntityManager getEntityManager(){
		if (em == null)
			em = factory.createEntityManager();
		return em;
	}
	
	public static void closeEntityManager(){
		em.close();
	}
	
	public static void closeEntityManagerFactory(){
		factory.close();
	}
	
	public static void iniciaTransacao(){
		em.getTransaction().begin();
	}
	
	public static void finalizaTransacao(){
		try {
			em.getTransaction().commit();
			
		} catch (Exception e) {
			
		}finally{
			em.close();
			em = null;
		}
		
	}

}
